package com.kugou.internal.compiler;

import com.kugou.internal.MiscUtils;
import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;

import java.io.File;
import java.io.IOException;

/**
 * Created by Alex on 2014/9/20
 */
public class CompilationUnitImpl implements ICompilationUnit {

    private File file;
    private String packageName;

    public CompilationUnitImpl(File file, String packageName) {
        this.file = file;
        this.packageName = packageName;
    }

    @Override
    public char[] getContents() {
        try {
            return MiscUtils.readFileToString(file).toCharArray();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public char[] getMainTypeName() {
        return file.getName().replace(".java", "").toCharArray();
    }

    @Override
    public char[][] getPackageName() {
        if (packageName.contains(".")) { // 带包名
            String[] pkgs = packageName.split("\\.");
            char[][] result = new char[pkgs.length][];
            for (int i = 0; i < pkgs.length; i++) {
                result[i] = pkgs[i].toCharArray();
            }
            return result;
        }
        return new char[][]{}; //default package
    }

    public boolean ignoreOptionalProblems() {
        return false;
    }

    @Override
    public char[] getFileName() {
        return this.file.getName().toCharArray();
    }
}
